/*
Copyright 2008, 2009 Joonas Lehtolahti

This file is part of Openfire-C.

Openfire-C is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Openfire-C is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Openfire-C.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "XfirePackets.h"

/*
 * XfirePHc_GCJoin
 *   Creates network packet for joining a chat room
 *   http://xfireplus.com/xfwiki/index.php/Packet_19_00_-_f4_4c
 * 
 * Parameters:
 *   gcid      Group chat id
 *   gcname    Chat room name
 *   rqindex   Request index number
 *   rtype     Room type (1 = normal, 3 = live video)
 *   pass      Password for password protected rooms
 *   autoname  Can the chat room name be changed by server (0/1)
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCJoin(non0string *gcid,
		non0string *gcname, unsigned int rqindex, unsigned int rtype,
		non0string *pass, unsigned int autoname
	) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cf4;
	
	/* Required parameters */
	if (!gcname) return NULL;
	
	/* Default values for parameters */
	if (!gcid) {
		gcid = n0salloc(XFP_SIZE_ATTRGCIDVALUE);
		memset(gcid->s, 0, gcid->l);
	}
	if (!pass) {
		pass = n0salloc(0);
	}
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 6, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x0b), XFP_TYPE_INT, &rqindex);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\xaa), XFP_TYPE_INT, &rtype);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x05), XFP_TYPE_STRING, gcname);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x5f), XFP_TYPE_STRING, pass);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\xa7), XFP_TYPE_BOOLEAN, &autoname);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCPart
 *   Creates network packet for leaving a chat room
 *   http://xfireplus.com/xfwiki/index.php/Packet_19_00_-_f5_4c
 * 
 * Parameters:
 *   gcid      Group chat id
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCPart(non0string *gcid) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cf5;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 1, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCMsg
 *   Creates network packet for sending a message to a chat room
 *   http://xfireplus.com/xfwiki/index.php/Packet_19_00_-_f6_4c
 * 
 * Parameters:
 *   gcid      Group chat id
 *   text      Chat message
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCMsg(non0string *gcid,
		non0string *text) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cf6;
	
	/* Required parameters */
	if (!gcid) return NULL;
	if (!text) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 2, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x2e), XFP_TYPE_STRING, text);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCRequestBasicInfo
 *   Creates network packet for requesting basic info about chat rooms
 *   http://xfireplus.com/xfwiki/index.php/Packet_19_00_-_fa_4c
 * 
 * Parameters:
 *   gcids     List of group chat IDs
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCRequestBasicInfo(struct XfirePattr4 *gcids) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cfa;
	
	/* Required parameters */
	if (!gcids) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 1, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_LIST, gcids);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCRequestDetailedInfo
 *   Creates network packet for requesting detailed chat room info
 *   http://xfireplus.com/xfwiki/index.php/Packet_19_00_-_06_4d
 * 
 * Parameters:
 *   gcid      Group chat id
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCRequestDetailedInfo(non0string *gcid) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4d06;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 1, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCInvite
 *   Creates network packet for inviting friends into chat room
 * 
 * Parameters:
 *   gcid      Group chat ID
 *   userids   List of userids
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCInvite(non0string *gcid,
		struct XfirePattr4 *userids) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cfc;
	
	/* Required parameters */
	if (!gcid) return NULL;
	if (!userids) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 2, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x18), XFP_TYPE_LIST, userids);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCAdminRename
 *   Creates network packet for renaming a chat room
 * 
 * Parameters:
 *   gcid      Group chat id
 *   gcname    New chat room name
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCAdminRename(non0string *gcid,
		non0string *gcname) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cf8;
	
	/* Required parameters */
	if (!gcid) return NULL;
	if (!gcname) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 2, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x05), XFP_TYPE_STRING, gcname);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCAdminMOTD
 *   Creates network packet for setting a welcome message
 * 
 * Parameters:
 *   gcid      Group chat id
 *   gcmotd    New welcome message
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCAdminMOTD(non0string *gcid,
		non0string *gcmotd) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4d0c;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Default values */
	if (!gcmotd) gcmotd = char2non((unsigned const char *)"");
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 2, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x2e), XFP_TYPE_STRING, gcmotd);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCAdminUserPerm
 *   Creates network packet for changing user permission level
 * 
 * Parameters:
 *   gcid      Group chat id
 *   userid    The user whose permission to alter
 *   gcperm    The new permission level
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCAdminUserPerm(non0string *gcid,
		unsigned int userid, unsigned int gcperm) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cf9;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 3, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x18), XFP_TYPE_INT, &userid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x13), XFP_TYPE_INT, &gcperm);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCSave
 *   Creates network packet for changing room "saved" status
 *   http://xfireplus.com/xfwiki/index.php/Packet_19_00_-_fd_4c
 * 
 * Parameters:
 *   gcid      Group chat id
 *   gcset     Boolean value (0/1)
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCSave(non0string *gcid, unsigned int gcset) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cfd;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 2, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x2a), XFP_TYPE_BOOLEAN, &gcset);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCAdminAllowVoice
 *   Creates network packet for changing voice chat allowance
 * 
 * Parameters:
 *   gcid      Group chat id
 *   gcset     Boolean value (0/1)
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 * 
 * Notes:
 *   this packet is of the old style as well...
 */
XFIREPHO XfirePHc_GCAdminAllowVoice(non0string *gcid,
		unsigned int gcset) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4d0d;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 2, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x1b), XFP_TYPE_INT, &gcset);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCAdminForceVoiceOff
 *   Creates network packet for forcing voice off
 * 
 * Parameters:
 *   gcid      Group chat id
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 * 
 * Notes:
 *   this is no longer needed either
 */
XFIREPHO XfirePHc_GCAdminForceVoiceOff(non0string *gcid) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4d04;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 1, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_GCAdminSetDefaultPerm
 *   Creates network packet for setting default permission level
 * 
 * Parameters:
 *   gcid      Group chat id
 *   gcperm    The new permission level
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCAdminSetDefaultPerm(non0string *gcid,
		unsigned int gcperm) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4d08;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 2, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x13), XFP_TYPE_INT, &gcperm);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}


/*
 * XfirePHc_GCAdminKick
 *   Creates network packet for kicking user from chat room
 *   http://xfireplus.com/xfwiki/index.php/Packet_19_00_-_fb_4c
 * 
 * Parameters:
 *   gcid      Group chat id
 *   userid    The user to kick
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GCAdminKick(non0string *gcid, unsigned int userid) {
	/* Needed variables */
	struct XfirePacket *p, *sp;
	unsigned int climsg = 0x4cfb;
	
	/* Required parameters */
	if (!gcid) return NULL;
	
	/* Packet '19 00', has 2 attributes and regular attribute names */
	p = XfirePHo_Init(0x0019, 2, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"climsg", XFP_TYPE_INT, &climsg);
		
		sp = XfirePHo_Init(0, 2, 1);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x04), XFP_TYPE_GCID, gcid);
		XfirePHo_AddAttribute(sp, SHORT_ATTRIBUTE(\x18), XFP_TYPE_INT, &userid);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_NPACKET, sp);
	
	/* Return completed packet */
	return p;
}
